<template>
  <Tree
    defaultExpandAll
    class="condition-tree"
    draggable
    :tree-data="conditionListRef"
    :field-names="fieldNames"
    v-bind="$attrs"
  >
    <template #switcherIcon>
      <div></div>
    </template>
    <template #title="item">
      <div class="content" @mouseenter="handleMouseEnter(item)">
        <div class="line-horiz" :style="lineHorizStyle(item)"></div>
        <div class="line-vert" :style="lineVertStyle(item)"></div>
        <!-- <div class="line-stand" :style="lineStandStyle(item)"></div> -->
        <div
          v-if="item.level < 3"
          class="level"
          :style="{
            color: `${levelList[item.level]}`,
            border: `1px solid ${levelList[item.level]}`,
          }"
        >
          {{ item.level + 1 }}
        </div>
        <div v-else class="level-max"></div>

        <div
          :id="`${item.conditionId}`"
          class="title condition-title"
          :style="{ maxWidth: ` ${285 - item.level * (item.level > 2 ? 38 : 40)}px` }"
        >
          <Tooltip :title="item.conditionName">
            {{ item.conditionName }}
          </Tooltip>
        </div>
        <div class="condition-button mr-2" v-if="isShow">
          <a class="mr-2" style="color: rgba(255, 82, 43, 1)" @click.stop="handleDelete(item)"
            >删除</a
          >
          <a
            class="mr-2"
            @click.stop="handleCreate(item)"
            v-if="item.conditionType === 1 || item.conditionType === 2"
            >新条件</a
          >
        </div>
        <div class="condition-button mr-2" v-else>
          <Dropdown>
            <template #overlay>
              <Menu>
                <MenuItem key="1">
                  <a
                    class="mr-2"
                    style="color: rgba(255, 82, 43, 1)"
                    @click.stop="handleDelete(item)"
                    >删除</a
                  >
                </MenuItem>
                <MenuItem key="2" v-if="item.conditionType === 1 || item.conditionType === 2">
                  <a class="mr-2" @click.stop="handleCreate(item)">新条件</a>
                </MenuItem>
              </Menu>
            </template>
            <div class="three-pie">
              <span class="pie mr-1"></span>
              <span class="pie mr-1"></span>
              <span class="pie mr-1"></span>
            </div>
          </Dropdown>
        </div>
      </div>
    </template>
  </Tree>
</template>
<script lang="ts" setup name="ConditionTree">
  import { Tree, Dropdown, Menu, MenuItem, Tooltip } from 'ant-design-vue';

  import { computed, ref } from 'vue';
  import { levelList } from './condition.data';
  import { useMessage } from '/@/hooks/web/useMessage';

  const props = defineProps({
    conditionList: {
      type: Array,
    },
  });
  const emit = defineEmits(['create', 'delete']);
  const fieldNames = ref({
    children: 'children',
    title: 'conditionName',
    key: 'conditionId',
  });
  const conditionListRef = computed<any>(() => {
    arrayFlagLevel(props.conditionList, 0);

    return props.conditionList;
  });

  const { createConfirm } = useMessage();

  function arrayFlagLevel(arr, level) {
    if (!arr || !arr.length) return;
    arr.forEach((item) => {
      item.level = level;
      if (item.children && item.children.length) {
        arrayFlagLevel(item.children, level + 1);
      }
    });
  }

  function lineHorizStyle(item) {
    const params: any = {
      width: `${
        (item.level ? item.level : 1 - 1) * 32 +
        12 * (item.level === 1 || item.level === 0 ? 0 : item.level - 1)
      }px`,
      marginLeft: `14px`,
      borderTop: `1px dashed #c9c9c9`,
    };
    if (item.level === 0) {
      params.marginLeft = 0;
    }

    return params;
  }
  function lineVertStyle(item) {
    const params: any = {
      position: 'absolute',
      height: `${item.level * 100}%`,
      borderLeft: '1px dashed #c9c9c9',
      left: '24px',
      bottom: '18px',
    };
    return params;
  }

  function handleCreate(item) {
    emit('create', item);
  }
  function handleDelete(item) {
    if (item.conditionType === 1 || item.conditionType === 2) {
      createConfirm({
        iconType: 'warning',
        title: '警告',
        content: `当前逻辑节点包含了多个条件，删除将不可撤回确认要删除吗？`,
        onOk: async () => {
          emit('delete', item);
        },
      });
    } else {
      emit('delete', item);
    }
  }
  const isShow = ref(false);

  function handleMouseEnter(item) {
    let width = 285 - item.level * (item.level > 2 ? 38 : 40);
    let titleList = document.getElementsByClassName('condition-title');
    let list = Array.from(titleList);

    const value = list.find((i) => i.id == item.conditionId);
    if (value && (value.clientWidth === width || value.clientWidth > width)) {
      isShow.value = false;
    } else {
      isShow.value = true;
    }
  }
</script>
<style lang="less" scoped>
  .condition-tree {
    width: 100%;

    .content {
      display: flex;
      align-items: center;
      height: 36px;
      width: 392px;
      padding-left: 12px;
      position: relative;

      &:hover .condition-button {
        display: block;
      }

      .title {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      //.line-horiz {
      // &::after {
      //   content: '';
      //   display: block;
      //   position: absolute;
      //   left: 12px;
      //   bottom: 12px;
      //   height: 50%;
      //   border-left: 1px dashed #c9c9c9;
      // }
      //}

      .level {
        width: 24px;
        height: 24px;
        line-height: 24px;
        opacity: 1;
        margin-right: 8px;
        border-radius: 50%;
        text-align: center;
      }

      .level-max {
        width: 8px;
        height: 8px;
        margin-right: 8px;
        margin-left: 4px;

        opacity: 1;
        border: 1px solid #ff522b;
        border-radius: 50%;
      }

      .condition-button {
        display: none;
        align-self: center;
        flex: 1;
        text-align: right;
        position: absolute;
        right: 5px;
        z-index: 120;

        .three-pie {
          width: 32px;
          height: 16px;

          border-radius: 0px 0px 0px 0px;
          opacity: 1;
          font-size: 16px;
          display: flex;
          justify-content: center;
          align-items: center;

          .pie {
            width: 4px;
            height: 4px;
            background: #c1c1c1;
            opacity: 1;
            border-radius: 50%;
          }
        }
      }
    }
    //border: 1px solid red;
  }
</style>
